<?php
/**
 * Object Relational Persistence Mapping Class for table: ticket_material
 *
 * @package	core.ordo
 * @author LHV
 * @version $Id$
 * @copyright 2009 LHV Company
 *
 */
$loader->requireOnce('ordo/Ticket.class.php');
class TicketMaterial extends ORDataObject {

	/**#@+
	 * Fields of table: ticket_material mapped to class members
	 */
	var $ticket_material_id	= '';
	var $ticket_id		    = '';
	var $material_id		= '';
	var $quantity_num		= '';
	var $quantity_title		= '';
	var $unit_price         = '';
	var $original_price		= '';
	var $insurance_price    = '';
	var $patient_price		= '';
	var $issue_date		    = '';
	var $issue_person		= '';
	var $doctor_id		    = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'ticket_material';

	/**
	 * Primary Key
	 */
	var $_key = 'ticket_material_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'TicketMaterial';

	/**
	 * Handle instantiation
	 */
	function TicketMaterial() {
		parent::ORDataObject();
	}

	/**#@+
     * Getters
     */
	public function getTicketId(){
        return $this->ticket_id;
    }

    public function getMaterialId(){
        return $this->material_id;
    }

    public function getQuantityNumber(){
        return $this->quantity_num;
    }

    public function getQuantityTitle(){
        return $this->quantity_title;
    }

    public function getUnitPrice(){
        return $this->unit_price;
    }

    public function getOriginalPrice(){
        return $this->original_price;
    }

    public function getInsurancePrice(){
        return $this->insurance_price;
    }

    public function getIssueDate(){
        return L10nHelper::renderDateTime($this->issue_date);
    }

    public function getIssuePerson(){
        return $this->issue_person;
    }

    public function getDoctorId(){
        return $this->doctor_id;
    }
    /**#@-*/
/**
     * Set data to the object.
     *
     * @param int $ticketId Ticket Id
     * @param int $matId Material Id
     * @param int $quanNum Material quantity number
     * @param string $quanTitle Material quantity title
     * @param mixed $unitPrice Material unit price
     * @param mixed $orgPrice Total price of the material
     * @param mixed $insPrice Total payment which Insurance Agent must paid for the material
     * @param mixed $patPrice Total payment which patient must paid for the material
     * @param mixed $issueDate Date issued material
     * @param mixed $issuePerson Peson who issued material
     * @param mixed $doctorId Treatment doctor
     */
    public function setData($ticketId, $matId, $quanNum, $quanTitle, $unitPrice, $orgPrice,
           $insPrice, $patPrice, $issueDate, $issuePerson, $doctorId){
       $data   =   array(
            'ticket_id'         =>    $ticketId,
            'material_id'       =>    $matId,
            'quantity_num'      =>    $quanNum,
            'quantity_title'    =>    $quanTitle,
            'unit_price'        =>    $unitPrice,
            'original_price'    =>    $orgPrice,
            'insurance_price'   =>    $insPrice,
            'patient_price'     =>    $patPrice,
            'issue_date'        =>    $issueDate,
            'issue_person'      =>    $issuePerson,
            'doctor_id'         =>    $doctorId
       );
       $this->populateArray($data);
    }

    public function getTicketMaterials($ticketId, $dateFrom, $dateTo, $filter){
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
                
        $dateFormat =   L10nHelper::getSqlDateFormat();
        $sql    =   array(
            'cols'  =>  "DATE_FORMAT(t.issue_date, '$dateFormat') as issue_date, t.material_id, m.$field as material_name,
                t.unit_price, t.original_price, t.insurance_price, t.patient_price, t.quantity_num, t.quantity_title,
                t.issue_person, p1.name as issue_person_name, t.doctor_id, p2.name as doctor",
            'from'  =>  "$this->_table t
                LEFT JOIN medical_material m ON t.material_id=m.medical_material_id                
                LEFT JOIN person p1 ON t.issue_person=p1.person_id
                LEFT JOIN person p2 ON t.doctor_id=p2.person_id",
            'where' =>  "t.ticket_id=$ticketId",
            'orderby'  => "issue_date, material_name");
        if($filter){
            $sql['where']  .=   " AND DATE(t.issue_date) >= '$dateFrom' AND DATE(t.issue_date) <= '$dateTo'";
        }
        $ds     =   & new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }

    /**
     * Delete all TicketMaterial item of a given ticket.
     *
     * @param int $ticketId Ticket Id
     */
    public function deleteItem($ticketId, $issueDate){
        $sql    =   "DELETE FROM $this->_table WHERE ticket_id='$ticketId' AND issue_date='$issueDate'";
        $this->_execute($sql);
    }

    /**
     * Get information on medical materials a patient has used in a resident examination at hospital.
     *
     * @param int $patientId Patient Id
     * @return array Result array
     */
    public function getMaterialByPatient($patientId) {
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $dateFormat =   L10nHelper::getSqlDateFormat();
        $status     =   Ticket::OPEN_STATUS;
        $sql    =   array(
            'cols'  =>  "DATE_FORMAT(t.issue_date, '$dateFormat') as issue_date, t.material_id, m.$field as material_name,
                t.unit_price, t.original_price, t.insurance_price, t.patient_price, t.quantity_num, t.quantity_title,
                t.issue_person, p1.name as issue_person_name, t.doctor_id, p2.name as doctor",
            'from'  =>  "$this->_table t
                INNER JOIN ticket tk ON t.ticket_id=tk.ticket_id
                LEFT JOIN medical_material m ON t.material_id=m.medical_material_id                
                LEFT JOIN person p1 ON t.issue_person=p1.person_id
                LEFT JOIN person p2 ON t.doctor_id=p2.person_id",
            'where' =>  "tk.patient_id=$patientId AND tk.status='$status'",
            'orderby'  => "t.issue_date ASC"
        );

        $ds     =   & new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }
}
?>
